﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visa.DB;
using System.Data;

namespace Visa.Service
{
    public class MenuService
    {
        private VisaEntities db = new VisaEntities();

        public int addOrUpdateMenu(MenuTin entity)
        {
            db.Entry(entity).State = entity.Id == 0 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
            return entity.Id;
        }

        public void DeleteMenu(int id)
        {
            var c = db.MenuTins.SingleOrDefault(l => l.Id == id);
            c.Active = false;
            db.SaveChanges();
        }

        public MenuTin GetMenuById(int id)
        {
            try
            {
                return db.MenuTins.SingleOrDefault(l => l.Id == id);
            }
            catch { return null; }
        }

        public List<MenuTin> GetAllMenu()
        {
            try
            {
                return db.MenuTins.Where(o => o.Active.Value == true).OrderBy( l => l.ParentId).ToList();
            }
            catch { return null; }
        }


        public int addOrUpdateTinOfMenu(TinofMenu entity)
        {
            db.Entry(entity).State = entity.Id == 0 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
            return entity.Id;
        }

        public void DeleteTinOfMenu(int id)
        {
            var c = db.TinofMenus.SingleOrDefault(l => l.Id == id);
            db.TinofMenus.Remove(c);
            db.SaveChanges();
        }

        public TinofMenu GetTinOfMenuById(int id)
        {
            try
            {
                return db.TinofMenus.SingleOrDefault(l => l.Id == id);
            }
            catch { return null; }
        }

        public List<TinofMenu> GetAllTinOfMenu()
        {
            try
            {
                return db.TinofMenus.ToList();
            }
            catch { return null; }
        }

        public List<TinofMenu> GetALlByCateMenuId(int cateid)
        {
            try
            {
                return db.TinofMenus.Where(o => o.CateId.Value == cateid).ToList();
            }
            catch { return null; }
        }
    }
}
